/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tabla.java
 *
 * Created on 9/02/2009, 09:33:20 AM
 */

package practica1;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Administrador
 */
public class Tabla extends javax.swing.JFrame {

    private JTable matriz;
    private JTable matrizND;
    private JTable matrizSim;
    private Container contenedor;

    /** Creates new form Tabla */
    public Tabla() {
        initComponents();
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

       contenedor = this.getContentPane();

    }

    public void pintarMatriz(Nodo aux, int filas, Vector b){

        String [] columna = new String [b.size()+3];
        int cont = 1;

        columna  [0] = "ESTADO";
        while(cont<b.size()+1){
            columna  [cont] = String.valueOf(b.elementAt(cont-1));
            cont++;
        }
        columna [cont] = "λ";

        cont++;
        columna [cont]= "ACEPTACION";
        String [][] datos = new String [filas+aux.getNumeroDeDatos()][b.size()+3];

        int fil =0;
        for(int row=1; row<filas;row++){
            Nodo pintar = aux.getNodo(row);
            for(int i = 1; i< b.size()+2; i++){
               if(columna[i].equals(String.valueOf(pintar.getDato1()))){
                    datos [fil][i]= String.valueOf(pintar.getLiga1().getName());
                    datos [fil][0] = String.valueOf(pintar.getName());
                    fil++;
                }

                if(pintar.getDato2()!= '-'){
                    
                    
                    if(columna[i].equals(String.valueOf(pintar.getDato2()))){
                        
                        datos [fil][i]= String.valueOf(pintar.getLiga2().getName());
                        datos [fil][0] = String.valueOf(pintar.getName());
                        fil++;
                    }
                }
            }
        }

        datos [fil][0] = String.valueOf(filas);
        datos [fil][b.size()+2] = "OK";

       matriz = new JTable(datos,columna);
       matriz.setEnabled(false);
       matriz.setPreferredScrollableViewportSize(new Dimension(500,100));
       JScrollPane scroll = new JScrollPane(matriz);
       scroll.setBounds(15,11,575,350);
       contenedor.add(scroll,BorderLayout.CENTER);

    }

    public void pintarMatrizND(Vector estados, Vector terminales, int filas){
        String [] columna = new String [terminales.size()+2];
        columna  [0] = "ESTADO";
        int cont = 1;
        while(cont<=terminales.size()){
            columna  [cont] = String.valueOf(terminales.elementAt(cont-1));
            cont++;
        }

        columna [cont]= "ACEPTACION";
        String [][] datos = new String [estados.size()][terminales.size()+2];

        for(int k=0; k<estados.size(); k++){
            Vector trans = (Vector)estados.elementAt(k);
            datos [k][0] = String.valueOf(k+1);
            for(int q =1; q<terminales.size()+1; q++){
                for(int r=1; r<trans.size(); r=r+2){
                    if(columna[q].equals(String.valueOf(trans.elementAt(r)))){
                        datos [k][q] = String.valueOf(trans.elementAt(r+1));
                    }
                }
            }
            Vector acepta = (Vector)trans.elementAt(0);
            for(int p=0; p<acepta.size(); p++){
                if((String.valueOf(acepta.elementAt(p))).equals(String.valueOf(filas))){
                    datos [k][cont] = "OK";
                }
            }
            
        }

        matrizND = new JTable(datos,columna);
        matrizND.setEnabled(false);
        simplificar(datos,columna,estados.size(),terminales.size()+2);




    }

    public void simplificar(String [][]datos,String [] titulos,int filas,int columnas){
        int igual=0;
        int indice=1;
        Vector iguales=new Vector();
        String [][] aux= new String[filas][columnas];
        for(int j=0;j<columnas;j++){
            aux[0][j]=datos[0][j];
        }
        for(int i=1;i<filas;i++){
            for(int j=0;j<indice;j++){

                    igual=0;
                    for(int l=1;l<columnas-1;l++){
                       if(aux[j][l]==null && datos[i][l]==null)igual++;
                       else if(aux[j][l]!=null && aux[j][l].equals(datos[i][l]))igual++;
                            else break;
                     }
                    if(igual==columnas-2){
                        iguales.add(datos[i][0]);
                        iguales.add(aux[j][0]);
 System.out.println("IGUALES  "+iguales);
                        break;
                    }
                }

                if(igual!=columnas-2){

                    for(int q=0;q<columnas;q++ ){
                        aux[indice][q]=datos[i][q];

                    }
                    indice++;
                }
            }
        for(int i=0;i<iguales.size();i+=2){
            for(int j=0;j<filas;j++){
                for(int q=0;q<columnas; q++){
                    if(aux[j][q]!=null && aux[j][q].equals(String.valueOf(iguales.elementAt(i)))){
                        aux[j][q]=String.valueOf(iguales.elementAt(i+1));
                    }
                }
            }
        }

        matrizSim = new JTable(aux,titulos);
        matrizSim.setEnabled(false);
       


    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCerrar = new javax.swing.JButton();
        convertirNoDeterministico = new javax.swing.JButton();
        sim = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(practica1.Practica1App.class).getContext().getResourceMap(Tabla.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        btnCerrar.setText(resourceMap.getString("btnCerrar.text")); // NOI18N
        btnCerrar.setName("btnCerrar"); // NOI18N
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        convertirNoDeterministico.setText(resourceMap.getString("convertirNoDeterministico.text")); // NOI18N
        convertirNoDeterministico.setName("convertirNoDeterministico"); // NOI18N
        convertirNoDeterministico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertirNoDeterministicoActionPerformed(evt);
            }
        });

        sim.setText(resourceMap.getString("sim.text")); // NOI18N
        sim.setEnabled(false);
        sim.setName("sim"); // NOI18N
        sim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(299, 299, 299)
                .addComponent(sim)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(convertirNoDeterministico, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCerrar, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(379, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(convertirNoDeterministico, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sim, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCerrarActionPerformed

    private void convertirNoDeterministicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertirNoDeterministicoActionPerformed
       new Tabla().removeAll();
       contenedor.remove(3);
        matrizND.setPreferredScrollableViewportSize(new Dimension(500,100));
        JScrollPane scroll = new JScrollPane(matrizND);
        scroll.setBounds(15,11,575,350);
        contenedor.add(scroll,BorderLayout.CENTER);
        convertirNoDeterministico.setEnabled(false);
         sim.setEnabled(true);
        //contenedor.repaint();
}//GEN-LAST:event_convertirNoDeterministicoActionPerformed

    private void simActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simActionPerformed
       new Tabla().removeAll();

       contenedor.remove(3);
       matrizSim.setEnabled(false);
       matrizSim.setPreferredScrollableViewportSize(new Dimension(500,100));
       JScrollPane scroll = new JScrollPane(matrizSim);
       scroll.setBounds(15,11,575,350);
       contenedor.add(scroll,BorderLayout.CENTER);
       sim.setEnabled(false);
}//GEN-LAST:event_simActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tabla().setVisible(true);
                
            }
        });
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton convertirNoDeterministico;
    private javax.swing.JButton sim;
    // End of variables declaration//GEN-END:variables

}
